#include <stdio.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_errno.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_adc.h"
#include "iot_fan_led.h"

#define LED_NAME_GPIO_8  8
#define LED_PWM_PORT_1 1
#define PWM_FREQ_DIVITION 64000
#define INFRARED_IO_GPIO_07  7
#define ADC_INFRARED_CHANNEL_3  3

static IotGpioValue g_infraredValue = IOT_GPIO_VALUE0;
static led_state ledState = LED_OFF;


/* 控制主控板上的三色灯 */
void LedInit(void)
{
    // 初始化GPIO-08引脚
    int ret = IoTGpioInit(LED_NAME_GPIO_8);
	if(ret != 0){ 
        printf("IoTGpioInit failed :%#x \r\n",ret);
		return;
    }

	/* 设置复用为PWM功能 */
	ret = IoTGpioSetFunc(LED_NAME_GPIO_8, IOT_GPIO_FUNC_GPIO_8_PWM1_OUT);
	if(ret != 0){ 
        printf("IoTGpioSetFunc failed :%#x \r\n",ret);
		return;
    }

	/* PWM初始化 */
    ret = IoTPwmDeinit(LED_PWM_PORT_1);
    if(ret != 0){ 
        printf("IoTPwmDeinit failed :%#x \r\n",ret);
		return;
    }

    ret = IoTPwmInit(LED_PWM_PORT_1);
    if(ret != 0){ 
        printf("IoTPwmInit failed :%#x \r\n",ret);
		return;
    }
	printf("----- LedPwmInit success! -----\r\n");
}

static void LedCtrl(int duty)
{
	/* PWM控制LED亮度 */
    IoTPwmStart(LED_PWM_PORT_1, duty, PWM_FREQ_DIVITION);
}



/* 初始化INFRARED模块 */
void InfraredGpioInit(void)
{
    // 初始化LED引脚
    int ret = IoTGpioInit(INFRARED_IO_GPIO_07);
	if(ret != 0){ 
        printf("IoTGpioInit failed :%#x \r\n",ret);
		return;
    }

	/* 设置复用为GPIO功能 */
	ret = IoTGpioSetFunc(INFRARED_IO_GPIO_07, IOT_GPIO_FUNC_GPIO_7_GPIO);
	if(ret != 0){ 
        printf("IoTGpioSetFunc failed :%#x \r\n",ret);
		return;
    }

    /* 设置方向为输入 */
	ret = IoTGpioSetDir(INFRARED_IO_GPIO_07, IOT_GPIO_DIR_IN);
    if (ret != 0) {
        printf("IoTGpioSetDir failed :%#x \r\n",ret); 
        return;
    }
	
    printf("----- InfraredGpioInit success! -----\r\n");
}

/* 检测红外传感器并控制LED */
void InfraredCtrlTask(void)
{
    u32 ret = KP_ERR_SUCCESS;

	while (1)
	{
    	/* 红外传感器引脚的默认值是低电平 */
    	ret = IoTGpioGetInputVal(INFRARED_IO_GPIO_07, &g_infraredValue);
    	if (ret != KP_ERR_SUCCESS) {
            printf("IoTGpioGetInputVal failed :%#x \r\n", ret); 
            return;
        }
        if (g_infraredValue == IOT_GPIO_VALUE1) {
			if (ledState != LED_ON) {
        		AgricultureLedCtrl(LED_ON);
				ledState = LED_ON;
    			printf("Some objects approaching, alert led on \r\n");
			}
        } else {
            if (ledState != LED_OFF) {
                AgricultureLedCtrl(LED_OFF);
				ledState = LED_OFF;
				printf("Objects leave, alert led off \r\n");
            }
        }
		osDelay(300);
	}
}


/* ADC检测并控制LED */
void AgricultureInfraredTask(void)
{
    unsigned short data = 0;
	unsigned int ret = HI_ERR_SUCCESS;

    LedInit();
	
    /* 循环检测红外并处理 */
    while (1) {
	    /* 读取 ADC 信息 */
	    if ((ret = IoTAdcRead(ADC_INFRARED_CHANNEL_3, &data, IOT_ADC_EQU_MODEL_4, IOT_ADC_CUR_BAIS_DEFAULT, 0)) == HI_ERR_SUCCESS) {
	        printf("ADC_VALUE = %d \r\n", (unsigned int)data);

	        if(data < 500){
                if (ledState != LED_ON) {
	                LedCtrl((2000-data) / 2000);
                    g_infraredValue = IOT_GPIO_VALUE1;
                    ledState = LED_ON;
                }
	        }else{
	            if (ledState != LED_OFF) {
	                LedCtrl(0);
                    g_infraredValue = IOT_GPIO_VALUE0;
                    ledState = LED_OFF;
                }
	        }
	    } else {
			printf("IoTAdcRead failed :%#x \r\n", ret);
	    }
		osDelay(100);
    }

    return;
}

int GetInfraredState(void)
{
    return g_infraredValue;
}

